//
//  SLChangeHeadeViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/6/10.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLChangeHeadeViewController.h"
#import "SLNetWork.h"



@interface SLChangeHeadeViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImageView *oldHeadView;


@end

@implementation SLChangeHeadeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  [self.view addSubview:self.oldHeadView];
  [self.oldHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.center.equalTo(self.view);
    make.size.mas_equalTo(CGSizeMake(60, 60));
  }];
  
  UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"保存"
                                                              style:UIBarButtonItemStylePlain target:self action:@selector(saveIcon)];
  self.navigationItem.rightBarButtonItem = rightBtn;
  
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  self.title = @"更换头像";
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)changeIcon{
  @weakify(self)
  UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"更换头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  sheet.title = nil;
  UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
  if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  }
  
  UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    @strongify(self)
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
    picker.delegate = self;
    picker.allowsEditing = YES;//设置可编辑
    picker.sourceType = sourceType;
    [self presentViewController:picker animated:YES completion:nil];//进入照相界面
  }];
  UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"本地获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    @strongify(self)
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
    picker.delegate = self;
    picker.allowsEditing = YES;//设置可编辑
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];//进入照相界面
  }];
  
  UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  
  [sheet addAction:action1];
  [sheet addAction:action2];
  [sheet addAction:action3];
  [self presentViewController:sheet animated:YES completion:nil];
}

- (void)saveIcon{
  @weakify(self)
  NSData *imgData = UIImageJPEGRepresentation(self.oldHeadView.image, 0.3);
//  NSData *img64Data = [imgData base64EncodedDataWithOptions:0];
  [SLNetWork upLoadImage:imgData sender:@"headPhoto" sendeName:@"userimage" complete:^(id respond, NSError *error) {
    if (!error) {
      @strongify(self)
      NSNotification *notify = [[NSNotification alloc] initWithName:kChangeHeadSuccess object:nil userInfo:@{@"userIcon":self.oldHeadView.image}];
      [[NSNotificationCenter defaultCenter] postNotification:notify];
      [self.navigationController popViewControllerAnimated:YES];
    }
  }];
  
}




#pragma mark - 照相机代理方法
//点击相册中的图片 货照相机照完后点击use  后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  @weakify(self);
  [picker dismissViewControllerAnimated:YES completion:^{
    @strongify(self)
    // 上传照片
    UIImage *icon = info[UIImagePickerControllerOriginalImage];
    self.oldHeadView.image = icon;
  }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (UIImageView *)oldHeadView{
  if (!_oldHeadView) {
    _oldHeadView = [[UIImageView alloc] init];
      [_oldHeadView sd_setImageWithURL:[NSURL URLWithString:self.oldHead]];
    _oldHeadView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeIcon)];
    [_oldHeadView addGestureRecognizer:tap];
    
  }
  return _oldHeadView;
}

@end
